package com.zyb.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zyb.eduservice.entity.EduSubject;
import com.zyb.eduservice.entity.excel.SubjectData;
import com.zyb.eduservice.entity.subject.OneSubject;
import com.zyb.eduservice.entity.subject.TwoSubject;
import com.zyb.eduservice.listener.SubjectExcelListener;
import com.zyb.eduservice.mapper.EduSubjectMapper;
import com.zyb.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testJava
 * @since 2022-04-01
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {
        //文件输入流
        try {
            InputStream inputStream = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getAllOneSubject() {
        //查询所有1级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id", "0");
        List<EduSubject> oneEduSubjects = baseMapper.selectList(wrapperOne);
        //查询所有2级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> twoEduSubjects = baseMapper.selectList(wrapperTwo);

        //创建list集合，用于存储最终封装数据
        List<OneSubject> finalSubjectList = new ArrayList<>();

        //封装查询过得1级分类
        //查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值，
        //封装到要求的list集合里面 List<OneSubject> finalSubjectList
        for (int i = 0; i < oneEduSubjects.size(); i++) {
            //得到oneSubjectList每个eduSubject对象
            EduSubject eduSubjectOne = oneEduSubjects.get(i);

            //把eduSubject里面值获取出来，放到OneSubject对象里面
            OneSubject oneSubject = new OneSubject();
            //eduSubject值复制到对应oneSubject对象里面
            //oneSubject.setId(eduSubject.getId());
            //oneSubject.setTitle(eduSubject.getTitle());
            BeanUtils.copyProperties(eduSubjectOne, oneSubject);
            //多个OneSubject放到finalSubjectList里面
            finalSubjectList.add(oneSubject);

            //在一级分类循环遍历查询所有的二级分类
            //创建list集合封装每个一级分类的二级分类
            List<TwoSubject> twoSubjectList = new ArrayList<>();

            for (int m = 0; m < twoEduSubjects.size(); m++) {
                //获取每个二级分类
                EduSubject eduSubjectTwo = twoEduSubjects.get(m);

                //判断二级分类parent_id和一级分类id是否一样
                if (eduSubjectTwo.getParentId().equals(eduSubjectOne.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    //把eduSubjectTwo值复制到twoSubject里面，放到twoSubjectList里面
                    BeanUtils.copyProperties(eduSubjectTwo, twoSubject);
                    twoSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoSubjectList);
        }
        return finalSubjectList;
    }
}
